package com.ctjsoft.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 测试基于线程池自定义线程
 * AbortPolicy 丢弃任务并抛出异常
 * DiscardPolicy 丢弃任务，但是不抛异常
 * DiscardOldestPolicy 丢弃队列最前面的任务，重新提交被拒绝的任务
 * CallerRunsPolicy 由提交任务的线程（调用线程）直接执行此任务
 *
 * @author: liaoyy
 * @date: Created in 2021/11/15 11:49
 */
public class ThreadPoolDemo1 {

    public static void main(String[] args) {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                /*
                 * AbortPolicy 丢弃任务并抛出异常
                 * DiscardPolicy 丢弃任务，但是不抛异常
                 * DiscardOldestPolicy 丢弃队列最前面的任务，重新提交被拒绝的任务
                 * CallerRunsPolicy 由提交任务的线程（调用线程）直接执行此任务
                 */
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        try {
            for (int i = 1; i <= 200; i++) {
                threadPoolExecutor.submit(() ->{
                    Thread thread = new Thread();
                    thread.start();
                    System.out.println(thread.getName());
                });
//                threadPoolExecutor.execute(() -> {
//                    System.out.println(Thread.currentThread().getName() + "办理业务");
//                });
            }
        }finally {
            threadPoolExecutor.shutdown();
        }





    }



}
